<?php
namespace app\admin\model;
use think\Db;
use think\Model;
use my\Security;
use app\admin\model\Franchisee as FranchiseeModel;

class Refer extends Model
{
     protected $franchisee_model;
	 public function __construct()
	{
		parent::__construct();
		$this->franchisee_model = new FranchiseeModel();
	}

	//推广人员列表
    public function referLis($page,$limit,$data){
        $where = [];
        $where[] = "a.status = 1 and a.is_del = 1";
        if(!empty($data['fid'])){
            $where[] = "a.fid = ".$data['fid'];
        }
        if (!empty($data['name'])) {
            $where[] = "a.name like '%" . rtrim($data['name']) . "%'";
        }
        if (!empty($data['user_phone'])) {
            $where[] = "b.user_phone like '%" . Security::encrypt(rtrim($data['phone'])) . "%'";
        }
        $whereStr = '';
        if ($where) {
            $whereStr = implode($where, ' and ');
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('refer')->alias('a')->join('user b','a.uid = b.uid')->where($whereStr)->count();
        $field = 'a.id,a.name,a.fid,a.uid,b.user_phone';
        $list = Db::name('refer')->alias('a')->join('user b','a.uid = b.uid')->where($whereStr)->limit($start, $limit)->field($field)->order('a.id','desc')->select();
        //推广人员计算数据信息
        $result = [];
        foreach($list as $key=>$value){
            $list[$key]['user_phone'] = Security::decrypt($value['user_phone']);
            //查询统计数据
            $result = $this->referCount($value['fid'],$value['uid']);
            //推荐人数
            $list[$key]['recomdNum'] = $result['recomdNum']?$result['recomdNum']:0;
            //顺风车认证人数
            $list[$key]['frAuthNum'] = $result['frAuthNum']?$result['frAuthNum']:0;
            //下单人数
            $list[$key]['orderNum']  = $result['orderNum']?$result['orderNum']:0;
            //订单数
            $list[$key]['ordersnNum']= $result['ordersnNum']?$result['ordersnNum']:0;
            //订单总额
            $list[$key]['orderMoney']= $result['orderMoney']?$result['orderMoney']:0.00;
            //转换率
            $list[$key]['switchNum'] = $result['switchNum'];
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }

    //推广人员查询数据信息
    public function referCount( $fid='' , $uid='' ){
        //查询推荐人数
        $result = [];
        $referList = DB::name('user')->where(['referee_id'=>$uid,'is_del'=>1])->field('uid,user_phone')->select();
        $result['recomdNum'] = count($referList);
        //判断推荐人数是否为空
        if(!empty($referList)){
            //推荐人数不为空 计算下单人数 、 订单数 、 订单总额
            $userStr = '';
            foreach ($referList as $key=>$value){
                $userStr .= $value['uid'].',';
            }
            $userStr = rtrim($userStr,',');
            //查询顺风车认证人数
            $result['frAuthNum'] = $this->franchisee_model->frAuthNum($userStr);
            //查询顺风车
            $frOrderInfo = $this->franchisee_model->frOrderInfo($userStr);
            //查询城际
            $interOrderInfo = $this->franchisee_model->interOrderInfo($userStr);
            //查询专车
            $xmOrderInfo = $this->franchisee_model->xmOrderInfo($userStr);
            //下单人数
            $userInfo = array_unique(array_merge($frOrderInfo['userid'],$interOrderInfo['userid'],$xmOrderInfo['userid']));
            $result['orderNum'] = count($userInfo);
            //订单数量
            $result['ordersnNum'] = $frOrderInfo['number'] + $interOrderInfo['number'] + $xmOrderInfo['number'];
            //订单总额
            $result['orderMoney'] = $frOrderInfo['need_price'] + $interOrderInfo['need_price'] + $xmOrderInfo['need_price'];
            //转化率
            $result['switchNum'] = floor(($result['orderNum']/$result['recomdNum'])*100).'%';
        }else{
            //推荐人数为空
            $result['frAuthNum'] = '0';//顺风认证人数
            $result['orderNum'] = '0';//下单人数
            $result['ordersnNum'] = '0';//订单数
            $result['orderMoney'] = '0';//订单总额
            $result['switchNum'] = '0%';//转化率
        }
        return $result;
    }
}
